const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

// pages/sysConfig/config_gs/gs_stem_type/gs_stem_type.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth:wx.getSystemInfoSync().windowWidth,
  },

  tap_item(e){
    let index = e.currentTarget.id
    let dic = this.data.lis[index]
    let row_id = dic.id
    let name = dic.name
    wx.showActionSheet({
      itemList: ['修改名称','修改封面','删除'],
      success: res => {
        if(res.tapIndex==0){
          this.update(row_id,name)
        }else if(res.tapIndex==1){
          this.chooseimg(index)
        }else if(res.tapIndex==2){
          this.delete(row_id, name)
        }
      },
    })
  },

  chooseimg(index){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFile = res.tempFiles[0]
        if(tempFile.size>100000){
          wx.showModal({
            content: '图片不可大于100KB',
            showCancel: false,
            title: '提示',
          })
        }else{
          wx.uploadFile({
            filePath: tempFile.tempFilePath,
            name: 'files',
            url: domainName + '/gs_stem_type_updatePic',
            formData: {
              row_id:this.data.lis[index].id,
            },
            header : {'content-type':'application/x-www-form-urlencoded'},
            success: res => {
              if(JSON.parse(res.data).update_res){
                wx.showToast({
                  title: '修改完成！',
                })
                let lis = this.data.lis
                lis[index].cover = tempFile.tempFilePath
                this.setData({lis})
              }
            },
          })
        }

      },
    })
  },

  delete(row_id, name){
    wx.showModal({
      content: '确定要删除【' + name + '】吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_stem_type_delete',
            data: {
              row_id,
            },...parameter,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '删除完成！',
                })
                this.setData({
                  lis:res.data.lis
                })
              }
            },
          })
        }
      },
    })
  },


  update(row_id, name){
    wx.showModal({
      content: name,
      editable: true,
      showCancel: true,
      title: '修改类型名称',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/gs_stem_type_update',
            data: {
              row_id:row_id,
              content:res.content
            },...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '修改完成！',
                })
                this.setData({
                  lis:res.data.lis
                })
              }
            },
          })
        }
      },
    })

  },

  add(){
    wx.navigateTo({
      url: '/pages/sysConfig/config_gs/add_gs_stem_type/add_gs_stem_type',
    })
    this.can_show = true
  },

  page_init(){
    wx.request({
      url: domainName + '/gs_stem_type_init',
      data: {
        
      },...parameter,
      success: res => {
        this.setData({
          lis:res.data.lis
        })
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.page_init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.can_show){
      this.page_init()
      this.can_show = false
    }
  },

})